﻿using Basic.FlowDAL;
using Basic.FlowModel.DB;
using Basic.FlowModel.Node;
using Basic.FlowRemoteModel;
using Basic.FlowRemoteModel.FlowNode.Model;
using WeDonekRpc.Helper;

namespace Basic.FlowCollect.lmpl
{
    internal class FlowNodeCollect : IFlowNodeCollect
    {
        private readonly IFlowNodeDAL _BasicDAL;

        public FlowNodeCollect ( IFlowNodeDAL basicDAL )
        {
            this._BasicDAL = basicDAL;
        }
        public void Set ( ToNoteBase[] datas )
        {
            this._BasicDAL.Set(datas);
        }
        public ToNoteBase[] GetsToNode ( long fromNodeId )
        {
            return this._BasicDAL.GetsToNode(fromNodeId);
        }
        public long Add ( FlowNodeAdd data )
        {
            string code = null;
            if ( data.NodeType == FlowNodeType.开始节点 )
            {
                code = "001";
            }
            else
            {
                code = ( int.Parse(this._BasicDAL.Max<string>(a => a.FlowId == data.FlowId, a => a.NodeCode)) + 1 ).ToString().PadLeft(3, '0');
            }
            return this._BasicDAL.Add(data, code);
        }
        public void Clear ( long flowId )
        {
            long[] ids = this._BasicDAL.Gets(a => a.FlowId == flowId, a => a.Id);
            if ( !ids.IsNull() )
            {
                this._BasicDAL.Delete(ids);
            }
        }

        public void Delete ( DBFlowNode sour )
        {
            this._BasicDAL.Delete(sour.Id);
        }

        public DBFlowNode Get ( long id )
        {
            return this._BasicDAL.Get(id);
        }

        public Result[] Gets<Result> ( long flowId ) where Result : class, new()
        {
            return this._BasicDAL.Gets<Result>(a => a.FlowId == flowId);
        }
        public bool SetForm ( DBFlowNode sour, FlowNodeForm set )
        {
            if ( sour.FormId == set.FormId && sour.IsAllowEdit == set.IsAllowEdit )
            {
                return false;
            }
            this._BasicDAL.Set(sour, set);
            return true;
        }
        public bool Set ( DBFlowNode sour, BeginNodeSet set )
        {
            if ( set.LimitTime.HasValue && set.LimitTime.Value == 0 )
            {
                set.LimitTime = null;
            }
            return this._BasicDAL.Update(sour, set);
        }
        public bool Set ( DBFlowNode sour, FlowNodeSet set )
        {
            if ( set.LimitTime.HasValue && set.LimitTime.Value == 0 )
            {
                set.LimitTime = null;
            }
            return this._BasicDAL.Update(sour, set);
        }
        public Dictionary<long, string> GetNames ( IEnumerable<long> nodeIds )
        {
            return this._BasicDAL.Gets(a => nodeIds.Contains(a.Id), a => new
            {
                a.Id,
                a.NodeName
            }).ToDictionary(a => a.Id, a => a.NodeName);
        }
        public void SetCoordinate ( DBFlowNode sour, ImgPoint point )
        {
            if ( sour.Coordinate != null && sour.Coordinate.X == point.X && sour.Coordinate.Y == point.Y )
            {
                return;
            }
            this._BasicDAL.SetCoordinate(sour, point);
        }

        public bool SetIntoOut ( DBFlowNode sour, FlowNodeIntoOut datum )
        {
            return this._BasicDAL.Update(sour, datum);
        }
        public Result Get<Result> ( long id ) where Result : class, new()
        {
            return this._BasicDAL.Get<Result>(id);
        }
        public Result[] Gets<Result> ( long[] ids ) where Result : class, new()
        {
            if ( ids.IsNull() )
            {
                return null;
            }
            return this._BasicDAL.Gets<Result>(ids);
        }

        public void SetIntoFormula ( long id, IntoFormula[] intoFormula )
        {
            this._BasicDAL.SetIntoFormula(id, intoFormula);
        }

        public long[] GetsFormId ( long flowId )
        {
            return this._BasicDAL.Gets(a => a.FlowId == flowId && a.FormId.HasValue, a => a.FormId.Value).Distinct().ToArray();
        }
    }
}
